package com.erdos.core.service.impl;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.erdos.common.service.BaseCRUDService;
import com.erdos.core.dao.FieldDefinitionDao;
import com.erdos.core.entity.FieldDefinition;
import com.erdos.core.service.FieldDefinitionService;

public class FieldDefinitionServiceImpl extends BaseCRUDService<FieldDefinition> implements
		FieldDefinitionService {
	private static final Log logger = LogFactory.getLog(FieldDefinitionServiceImpl.class);
	private FieldDefinitionDao fieldDefinitionDao;

	/**
	 * 验证指定的field id在数据库中是否已经存在
	 * @param fielded
	 * @author wangsw
	 * @since 20080717
	 */
	public boolean checkFieldIdExit(String fieldId) {	
		boolean flag;
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("fieldId", fieldId);
		FieldDefinition fd = getFieldDefinitionDao().findUniqueByParams(params);
		
		if (fd==null) {
			flag= false;
		} else {
			flag= true;
		}
		return flag;
	}

	/**
	 * 修改Field引用时调用，看修改后的Field定义是否可以兼容修改前的Field
	 * @param oldFieldSid
	 * @param newFieldSid
	 * @author sujk
	 * @since 200807
	 */
	public boolean isCompatible(Long oldFieldSid, Long newFieldSid) {
		FieldDefinition oldFd = fieldDefinitionDao.findByPrimaryKey(oldFieldSid);
		FieldDefinition newFd = fieldDefinitionDao.findByPrimaryKey(newFieldSid);

		if ((oldFd == null) || (newFd == null)) {
			String str = "Specified fieldSid not exist.";
			logger.warn(str);
			throw new IllegalArgumentException(str);
		}
		
		if (newFd.getDataTypeDb().equals("DATE")
				&& oldFd.getDataTypeDb().equals("DATE")) {
			return true;
		}
		/*
		 * 判断新对象的数据类型是否为 VARCHAR2 类型
		 */
		if (newFd.getDataTypeDb().equals("VARCHAR2")
				&& oldFd.getDataTypeDb().equals("VARCHAR2")) {
			if (newFd.getLen() >= oldFd.getLen()) {
				return true;
			}
		}
		/*
		 * 判断新对象的数据类型是否为 NUMBER 类型
		 */
		if (newFd.getDataTypeDb().equals("NUMBER")
				&& oldFd.getDataTypeDb().equals("NUMBER")) {
			/*
			 * 判断当新对象的长度大于或等于原对象长度
			 * 是，return true
			 * 否，return false
			 */
			if (newFd.getDecimalLength() == null) {
				newFd.setDecimalLength(0L);
			}
			if (oldFd.getDecimalLength() == null) {
				oldFd.setDecimalLength(0L);
			}
			if ((newFd.getDecimalLength() != 0)
					&& (oldFd.getDecimalLength() != 0)) {

				if (newFd.getDecimalLength() >= oldFd.getDecimalLength()) {

					if ((newFd.getLen() - newFd.getDecimalLength()) >= (oldFd
							.getLen() - oldFd.getDecimalLength())) {
						return true;
					}
				}
			}
			if (newFd.getDecimalLength() != 0 && oldFd.getDecimalLength() == 0) {
				if ((newFd.getLen() - newFd.getDecimalLength()) >= oldFd
						.getLen()) {
					return true;
				}
			}
			if ((newFd.getDecimalLength() == 0) && (oldFd.getDecimalLength() == 0)) {
				if (newFd.getLen() >= oldFd.getLen()) {
					return true;
				}
			}
		}
		return false;
		
	}

	public FieldDefinitionDao getFieldDefinitionDao() {
		return fieldDefinitionDao;
	}

	public void setFieldDefinitionDao(FieldDefinitionDao fieldDefinitionDao) {
		this.fieldDefinitionDao = fieldDefinitionDao;
	} 

}
